--
-- Author: yawei.zhu
-- Date: 2018-05-02 14:14:39
--
-- FirstTaskTargetRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskListNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())
    self._ui["Panel/dot"]:setVisible(false)
    self._ui.selected_btn:setTouchEnabled(false)
    self._ui["Panel"]:addClickEventListener(function()
        -- if self:isSelected() then return end

        self:setSelected(true)
        executeFunc(self._onClick, self)
    end)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._ui.name:setString(self._data.name)
    end
end

function ClassRef:isSelected()
    return not self._ui.selected_btn:isBright()
end

function ClassRef:setSelected(flag)
    self._ui["Panel"]:setOpacity(0)

    if flag then
        self._ui.img_1:setVisible(true)
        self._ui.img_2:setVisible(false)
        self._ui.name:setTextColor(cc.c3b(255, 225, 132)) 
        if #self._data.subDatas > 0 then
            self._ui["Panel"]:setOpacity(255)
        end
    else
        self._ui.img_1:setVisible(false)
        self._ui.img_2:setVisible(true)
        self._ui.name:setTextColor(cc.c3b(109, 109, 109))
    end

    if self._data and #self._data.subDatas <= 0 then
        self._ui.img_1:setVisible(false)
        self._ui.img_2:setVisible(false)
    end

    self._ui.selected_btn:setBright(not flag)
end

function ClassRef:setClickHandler(func)
    self._onClick = func
end

function ClassRef:setRedPoint(isShow)
    local posX, posY = self._ui["Panel/dot"]:getPosition()

    local panel = self._ui["Panel"]
    local flagNode = panel:getChildByName("_new_flag_node")

    if not isShow then
        if flagNode then
            flagNode:removeFromParent()
        end
    else
        if not flagNode then
            flagNode = cc.CSLoader:createNode(Res.NewFunctionFlagNode)
            flagNode:setName("_new_flag_node")
            panel:addChild(flagNode)
            flagNode:setPosition(cc.p(posX, posY))
        end
        flagNode:getTimeline():gotoFrameAndPlay( 0, flagNode:getTimeline():getDuration(), true )
    end

end

--是否设置为展开
function ClassRef:setUnfold(isUnfold)
    self._isUnfold = isUnfold
    if isUnfold then
        --如果是展开
        if self._data and #self._data.subDatas > 0 then
            self._ui["Panel/img_1"]:setVisible(true)
            self._ui["Panel/img_2"]:setVisible(false)
        end
    else
        --如果是关闭
        if self._data and #self._data.subDatas > 0 then
            self._ui["Panel/img_1"]:setVisible(false)
            self._ui["Panel/img_2"]:setVisible(true)
        end
    end
end

--列表当前是否是展开
function ClassRef:isUnfold()
    return self._isUnfold
end

return ClassRef
